package com.ponhu.ea2.stockcenter.provider.service.entity.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ponhu.ea2.stockcenter.dto.StockBatchQueryDTO;
import com.ponhu.ea2.stockcenter.dto.WMSInventoryInfo;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryBatchRecord;
import com.ponhu.ea2.stockcenter.provider.mapper.StockInventoryBatchRecordMapper;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockInventoryBatchRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StockInventoryBatchRecordServiceImpl
        extends ServiceImpl<StockInventoryBatchRecordMapper, StockInventoryBatchRecord>
        implements StockInventoryBatchRecordService {

    @Override
    public List<StockInventoryBatchRecord> getBatchRecordList(List<WMSInventoryInfo> inventoryInfoList) {

        List<String> inventoryCodes = inventoryInfoList.parallelStream()
                .map(WMSInventoryInfo::getInventoryCode).collect(Collectors.toList());
        List<String> batchCodes = inventoryInfoList.parallelStream()
                .map(WMSInventoryInfo::getBatchCode)
                .filter(StringUtils::isNotEmpty).collect(Collectors.toList());

        LambdaQueryWrapper<StockInventoryBatchRecord> queryWrapper = Wrappers
                .lambdaQuery(StockInventoryBatchRecord.class)
                .in(StockInventoryBatchRecord::getInventoryCode, inventoryCodes)
                .in(StockInventoryBatchRecord::getBatchCode, batchCodes);

        return list(queryWrapper);
    }

    @Override
    public IPage<StockInventoryBatchRecord> getStockBatchListByInventoryCode(StockBatchQueryDTO queryDTO) {

        LambdaQueryWrapper<StockInventoryBatchRecord> queryWrapper = Wrappers
                .lambdaQuery(StockInventoryBatchRecord.class)
                .eq(StockInventoryBatchRecord::getInventoryCode, queryDTO.getInventoryCode());

        Page queryPageInfo = new Page(queryDTO.getPageIndex(), queryDTO.getPageSize());
        return page(queryPageInfo, queryWrapper);
    }

}
